package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.user.UserAddress;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.user.client.UserAddressFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 订单管理  页面微服务
 *  购物车微服务
 *  用户微服务
 */
@Controller
public class OrderController {


    @Autowired
    private UserAddressFeignClient userAddressFeignClient;
    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private OrderFeignClient orderFeignClient;

    //去结算页面
    @GetMapping("trade.html")
    public String trade(Model model, HttpServletRequest request){
        //查询结算需要用到数据
        //1:根据用户ID 查询收货地址集合
        String userId = AuthContextHolder.getUserId(request);
        List<UserAddress> userAddressList = userAddressFeignClient.findUserAddressListByUserId(Long.parseLong(userId));
        model.addAttribute("userAddressList",userAddressList);//远程调用service-user微服务 gmall-user库 收货地址表

        //2:结算页面 结算号  防止二次提交
        Result result1 = orderFeignClient.trade();
        model.addAttribute("tradeNo",result1.getData());
        //3:查询商品清单 总件数 总金额
        Result<Map<String, Object>> result = cartFeignClient.getCartCheckedList(Long.parseLong(userId));
        model.addAllAttributes(result.getData());
        //进入到结算页面
        return "order/trade";
    }
}
